package reader;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.xml.bind.DatatypeConverter;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.shhic.itsed.mws.deviceintf.AntennaProtocol;
import com.shhic.itsed.mws.deviceintf.RawTagBank;
import com.shhic.itsed.mws.deviceintf.RawTagField;
import com.shhic.itsed.mws.deviceintf.ReaderCommand;
import com.shhic.itsed.mws.deviceintf.ReaderCommandType;
import com.shhic.itsed.mws.deviceintf.ReaderLinkStatusEnum;
import com.tjkcht.midware.reader.Rd30Manager;

import rdp_msg.Rdp.rdp_request;
import rdp_msg.Rdp.rdp_request.HeadType;

public class ReaderManagerTest3 {
	private String ip1 = "192.168.11.180";
	private String ip2 = "192.168.11.181";

	private int port = 5084;
	private Rd30Manager rm;
	private ReaderCommand cmd1;
	private ReaderCommand cmd2;
	public ResultCallBack call1 = new ResultCallBack();
	public ResultCallBack call2 = new ResultCallBack();

	@Before
	public void setUp() throws Exception {
		rm = new Rd30Manager();
		cmd1 = getCmd(ip1, 1);
		cmd2 = getCmd(ip2, 2);
	}

	private ReaderCommand getCmd(String ip, int ann) {
		ReaderCommand cmd = new ReaderCommand();
		cmd.setReaderIp(ip);
		cmd.setReaderPort(port);
		cmd.setReaderCommandType(ReaderCommandType.SINGLE);
		Set<AntennaProtocol> antennaProtocolSet = new HashSet<AntennaProtocol>();
		AntennaProtocol ap = new AntennaProtocol();
		ap.setAntennaNo(ann);
		antennaProtocolSet.add(ap);
		Set<String> plSet = new HashSet<String>();
		plSet.add("EPC");
		ap.setProtocols(plSet);
		cmd.setAntennaProtocolSet(antennaProtocolSet);
		List<RawTagField> readTagFieldList = new ArrayList<RawTagField>();
		RawTagField field = new RawTagField();
		field.setBank(RawTagBank.EPCBANK);
		field.setOffset(0);
		field.setLength(2);
		readTagFieldList.add(field);
		cmd.setReadTagFieldList(readTagFieldList);
		return cmd;
	}

	@After
	public void tearDown() throws Exception {
		rm.close();
	}

	@Test
	public void test() {

		try {
			// assertEquals(ReaderLinkStatusEnum.CONNECTING,
			// rm.queryDeviceLinkstatus(ip1));
			// Thread.sleep(1000);
			// assertEquals(ReaderLinkStatusEnum.CONNECTING,
			// rm.queryDeviceLinkstatus(ip2));
			/// send reader config
			// Thread.sleep(1000);

			// Thread.sleep(1000);
			for (int i = 0; i < 20; i++) {
				rm.initReader(ip1, port);
				rm.initReader(ip2, port);
				rm.dispatchReadCommand(ip2, cmd2, 2, call2);
				rm.dispatchReadCommand(ip1, cmd1, 2, call1);
				System.out.println("time1=");
				 Thread.sleep(2000);
				rm.stopReaderCommand(ip1);
				System.out.println("time2=");
				// Thread.sleep(1000);
				rm.stopReaderCommand(ip2);
				rm.disconnectReader(ip1);
				rm.disconnectReader(ip2);
				System.out.println("time="+i);
			}
			
			rm.dispatchReadCommand(ip2, cmd2, 2, call2);
			System.out.println("time2=xxxxx");
			Thread.sleep(2000);
			System.out.println("time2=yyyyyy");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
